﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;
using DevComponents.DotNetBar;
using DevComponents.DotNetBar.Metro;

namespace Logistics
{
    public partial class frmDepartmentsModify : Form
    {
        public delegate void ModelEventHandler(Model.tc_Department model);
        public ModelEventHandler OnModelEvent;
        private Model.tc_Department Department;
        public frmDepartmentsModify()
        {
            InitializeComponent();
        }
        public frmDepartmentsModify(Model.tc_Department _department)
        {
            InitializeComponent();
            Department = _department;
        }

        private void butSave_Click(object sender, EventArgs e)
        {
            if (!VerifyFrom())
                return;
            var model = new Model.tc_Department();
            model.Department = txtName.Text.Trim();
            model.Description = txtMemo.Text.Trim();
            var msg = "确定要保存'" + txtName.Text + "'吗？";
            if (MessageBox.Show(msg, "提示", MessageBoxButtons.YesNo, MessageBoxIcon.Information, MessageBoxDefaultButton.Button2) == DialogResult.Yes)
            {
                var objDepartmentInfo = new Manager.tc_Department();
                if (Department == null)
                {
                    model.Id = Guid.NewGuid();
                    objDepartmentInfo.Add(model);
                }
                else
                {
                    model.Id = Department.Id;
                    objDepartmentInfo.Update(model);
                }
                OnModelEvent?.Invoke(model);
                MessageBox.Show("数据保存成功", "提示", MessageBoxButtons.OK, MessageBoxIcon.Information);
                this.Close();
            }
        }
        private bool VerifyFrom()
        {
            var msg = string.Empty;
            if (string.IsNullOrEmpty(txtName.Text.Trim()))
            {
                msg = "部门名称不能为空";
                MessageBox.Show(msg, "提示", MessageBoxButtons.OK, MessageBoxIcon.Information);
                txtName.Focus();
                return false;
            }
            return true;
        }
        private void butCancel_Click(object sender, EventArgs e)
        {
            var msg = "数据尚未保存，确认要取消吗？";
            if (MessageBox.Show(msg, "警告", MessageBoxButtons.YesNo,
                MessageBoxIcon.Warning, MessageBoxDefaultButton.Button2) == DialogResult.Yes)
            {
                this.Close();
            }
        }

        private void frmDepartmentInfoModify_Load(object sender, EventArgs e)
        {
            if (Department != null)
                DataBind();
        }
        private void DataBind()
        {
            txtName.Text = Department.Department;
            txtMemo.Text = Department.Description;
        }
    }
}
